const app = getApp()
var CusBase64 = require('../../utils/base64.js');

Page({
  data: {
    dateStart: '',
    dateEnd: '',
    title:'',
    detail:'', 
    num: 1,
    id:'',
    realName:'',
    canOrder:true,
    // 使用data数据对象设置样式名  
    minusStatus: 'disabled'
  },
  onLoad: function (e) {
    var that = this
    console.log(1111+getApp().globalData)
    console.log(e)
    wx.request({
      url: getApp().data.service + '/menu/getMenuDetail',
      data: {
        id: e.id,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        var timestamp = Date.parse(new Date());
        if (timestamp > res.data.data.endTime || timestamp < res.data.data.startTime)
        {
          console.log(timestamp)
          wx.showToast({
            title: '当前时间不可点餐',
            icon:'none'
          })
          that.setData({
            canOrder:false
          })
        }
        that.setData({
          title: CusBase64.CusBASE64.decoder(CusBase64.CusBASE64.decoder(res.data.data.title)),
          detail: CusBase64.CusBASE64.decoder(CusBase64.CusBASE64.decoder(res.data.data.desc)),
          dateStart: formatTime(res.data.data.startTime, "Y-M-D h:m"),
          dateEnd: formatTime(res.data.data.endTime, "Y-M-D h:m"),
          id: res.data.data.id,
          realName:getApp().globalData.realName,
        })
      }
    })
  },

  inputName: function (e) {
    this.setData({
      realName:e.detail.value
    })
  },

  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1  
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回  
    this.setData({
      num: num
    });
  },
  

  confirmAction: function () {
    if(!this.data.canOrder){
      wx.showToast({
        title: '当前时间不可点餐',
        icon: 'none'
      })
      return
    }
    if (this.data.realName.length == 0) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
      return
    }
    var that = this
    if (getApp().globalData.realName == that.data.realName)
    {
      wx.request({
        url: getApp().data.service + '/menu/makeOrder',
        data: {
          menuId: that.data.id,
          userId: getApp().globalData.userid,
          orderCount: that.data.num
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '订餐成功',
            icon: 'success',
            duration: 2000,
            complete: function () {
              setTimeout(function () {
                wx.switchTab({
                  url: "../me/me",
                  success: function () {
                    getApp().globalData.isFormShare = true
                  }
                })
              }, 1500) //延迟时间 这里是1秒
            }
          })
        }
      })
    }
    else{
      getApp().globalData.realName = that.data.realName
      wx.request({
        url: getApp().data.service + '/menu/updateUser',
        data: {
          nickName: CusBase64.CusBASE64.encoder(getApp().globalData.userInfo.nickName),
          id: getApp().globalData.userid,
          realName: CusBase64.CusBASE64.encoder(that.data.realName)
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          wx.request({
            url: getApp().data.service + '/menu/makeOrder',
            data: {
              menuId: that.data.id,
              userId: getApp().globalData.userid,
              orderCount: that.data.num
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res)
              wx.showToast({
                title: '订餐成功',
                duration: 2000,
                complete: function () {
                  setTimeout(function () {
                    wx.switchTab({
                      url: "../me/me",
                      success: function () {
                        getApp().globalData.isFormShare = true
                      }
                    })
                  }, 1500) //延迟时间 这里是1秒
                }
              })
            },
            fail:function(res){
              wx.showToast({
                title: '订餐失败',
                icon: 'none',
                duration: 2000,
              })
            }
          })
        }
      })
    }
  },
})

function formatTime(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
